﻿#define _CRT_SECURE_NO_WARNINGS 1

#include"Contact.h"

//初始化
void ConInit(Contact* con)
{
	assert(con);
	con->arr = NULL;
	con->size = 0;
	con->capacity = INIT_CAPACITY;

	con->arr = (SLDatatype*)malloc(INIT_CAPACITY * sizeof(SLDatatype));
	if (con->arr == NULL)
	{
		perror("malloc");
		return;
	}
}

//销毁
void ConDestroy(Contact* con)
{
	assert(con);
	free(con->arr);
	con->arr = NULL;
	con->size = 0;
	con->capacity = 0;
}

//扩容
void CheckCapacity(Contact* con)
{
	assert(con);
	if (con->size == con->capacity)
	{
		size_t newCapacity = 2 * con->capacity;
		SLDatatype* tmp = (SLDatatype*)realloc(con->arr,newCapacity * sizeof(SLDatatype));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		con->arr = tmp;
		tmp = NULL;
		con->capacity = newCapacity;
	}
}


//增加通讯录数据
void AddContact(Contact* con)
{
	assert(con);
	perinfo info;
	printf("输入联系人的姓名:");
	scanf("%s", info.name);
	printf("输入联系人的性别:");
	scanf("%s", info.sex);
	printf("输入联系人的年龄:");
	scanf("%d", &(info.age));
	printf("输入联系人的电话:");
	scanf("%s", info.tel);
	printf("输入联系人的地址:");
	scanf("%s", info.addr);
	CheckCapacity(con);
	con->arr[con->size] = info;
	con->size++;
}

//通过名字查找联系人
int FindByName(Contact* con, char name[])
{
	assert(con);
	for (size_t i = 0; i < con->size; i++)
		{
			if (0 == strcmp(name, con->arr[i].name))
			{
				return (int)i;
			}
		}
	return -1;
}

//通过下标删除联系人
void ErasePerInfo(Contact* con, size_t pos)
{
	assert(con);
	assert(pos >= 0 && pos < con->size);
	for (size_t i = pos;i < con->size - 1; i++)
	{
		con->arr[i] = con->arr[i + 1];//arr[pos] = arr[pos+1]  arr[size-2] = arr[size-1]
	}
	con->size--;
}

//删除通讯录数据
void DelContact(Contact* con)
{
	assert(con);
	assert(con->size);
	char name[MAX_NAME];
	printf("输入要删除联系人的姓名:");
	scanf("%s", name);
	int find = FindByName(con, name);
	if (find < 0)
	{
		printf("要删除的联系人不存在,删除失败!\n");
		return;
	}
	ErasePerInfo(con, find);
	printf("删除成功!\n");
}
//展⽰通讯录数据
void ShowContact(Contact* con)
{
	assert(con);
	if (0 == con->size)
	{
		printf("通讯录为空...!\n");
		return;
	}
	else
	{
		printf("%-10s%-7s%-7s%-15s%s\n", "姓名", "性别", "年龄", "电话", "地址");
		for (size_t i = 0; i < con->size; i++)
		{
			printf("%-10s%-7s%-7d%-15s%s\n",
				con->arr[i].name,
				con->arr[i].sex,
				con->arr[i].age,
				con->arr[i].tel,
				con->arr[i].addr);
		}
	}
}
//查找通讯录数据
void FindContact(Contact* con)
{
	assert(con);
	char name[MAX_NAME];
	printf("输入查询的名字:");
	scanf("%s", name);
	int i = FindByName(con, name);
	if (i < 0)
	{
		printf("没有此联系人!\n");
		return;
	}
	printf("%-10s%-7s%-7d%-15s%s\n",
		con->arr[i].name,
		con->arr[i].sex,
		con->arr[i].age,
		con->arr[i].tel,
		con->arr[i].addr);
}
//修改通讯录数据
void ModifyContact(Contact* con)
{
	assert(con);
	char name[MAX_NAME];
	printf("输入要修改联系人的姓名:");
	scanf("%s", name);
	int find = FindByName(con, name);
	if (find < 0)
	{
		printf("该联系人不存在,修改失败!\n");
		return;
	}

	printf("输入新的姓名:");
	scanf("%s", con->arr[find].name);

	printf("输入新的姓别:");
	scanf("%s", con->arr[find].sex);

	printf("输入新的年龄:");
	scanf("%d", &(con->arr[find].age));

	printf("输入新的电话:");
	scanf("%s", con->arr[find].tel);

	printf("输入新的地址:");
	scanf("%s", con->arr[find].addr);

	printf("修改成功!\n");
}

















